import { DependencyContainer } from "tsyringe";
import { Record } from "../../core/Record";
import * as tokens from '../../tokens'
import * as utils from '../../utils'
export type TokensType = typeof tokens;
export type UtilsType = typeof utils;


function onKwai(container: DependencyContainer, tokens: TokensType, utils: UtilsType, record: Record) {
    record.log(`start ${new Date().getTime()}`)
    function clickKwaiHome() {
        // 我是一个注释
        const nodes = utils.getCurrentViewNodes(() => selector().filter(node => {
            const text = node.text()
            if (text.includes('我')) {
                return true
            }
            if (text.includes('精选')) {
                return true;
            }
            if (text.includes('消息')) {
                return true;
            }
            if (text.includes('首页')) {
                return true;
            }
            return false;
        }))
        record.log(`nodes size ${nodes.length}`)
        if (nodes.length > 0) {
            const selectedNode = nodes.find(n => n.selected())
            if (selectedNode) {
                record.log(selectedNode.text())
                const selectedNodeText = selectedNode.text();
                if (selectedNodeText !== '首页') {
                    const homeNode = nodes.find(node => node.text() == '首页')
                    if (homeNode) {
                        record.log(`home node clickable is ${typeof homeNode} ${typeof homeNode.clickable()}`)
                        utils.clickButton(homeNode)
                    }
                }
            } else {
                record.log(`没有选中的文字`)
            }
        }
    }
    clickKwaiHome()

    record.log(`进入首页直播页面`)
    function clickKwaiLive() {
        const nodes = utils.getCurrentViewNodes(() => selector().filter(node => {
            const text = node.desc()
            if (!text) return false;
            if (text.includes('关注')) {
                return true
            }
            if (text.includes('发现')) {
                return true;
            }
            if (text.includes('同城')) {
                return true;
            }
            if (text.includes('直播')) {
                return true;
            }
            return false;
        }))
        const selectedNode = nodes.find(it => it.selected())
        if (selectedNode) {
            const selectedNodeText = selectedNode.desc()
            if (selectedNodeText !== '直播') {
                const liveNode = nodes.find(it => it.desc() === `直播`)
                if (liveNode) {
                    utils.clickButton(liveNode)
                }
            }
        }
        const texts = nodes.map(node => node.desc())
        record.log(`node length is ${nodes.length} ${texts.join('\t')}`)
    }

    clickKwaiLive()

    record.log(`随机进入直播间`)

    function randomJoinLive() {

        const nodes = utils.getCurrentViewNodes(() => selector().filter(it => {
            const text = it.text()
            if (!text) return false;
            if (text === '直播中') {
                return true;
            }
            return false;
        }))
        if (nodes.length > 0) {
            const idx = random(0, nodes.length - 1)
            const node = nodes[idx]
            utils.clickButton(node)
        }
    }

    randomJoinLive()
    record.log(`finish`)

    return { success: true }
}